package de.openkanban.client.ui.adminpanel;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Encoding;
import com.smartgwt.client.types.FormMethod;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.FormItemIfFunction;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.HeaderItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

import de.openkanban.client.util.StringUtils;

/**
 * GUI Element for the NewBoardWindow
 */
public class NewBoardWindow extends Window {

	private ButtonItem createBoard = null;

	public NewBoardWindow() {
		// form
		final DynamicForm form = new DynamicForm();
		form.setHeight100();
		form.setWidth100();
		form.setPadding(3);
		form.setColWidths("60%", "40%");
		form.setLayoutAlign(VerticalAlignment.BOTTOM);

		final TextItem activity1 = new TextItem();
		activity1.setName("Akt1");
		activity1.setTitle("Name Aktivität 1");
		activity1.setRequired(true);

		final TextItem activity2 = new TextItem();
		creatActivity(activity2, 2);
		final TextItem activity3 = new TextItem();
		creatActivity(activity3, 3);
		final TextItem activity4 = new TextItem();
		creatActivity(activity4, 4);
		final TextItem activity5 = new TextItem();
		creatActivity(activity5, 5);
		final TextItem activity6 = new TextItem();
		creatActivity(activity6, 6);
		final TextItem activity7 = new TextItem();
		creatActivity(activity7, 7);
		final TextItem activity8 = new TextItem();
		creatActivity(activity8, 8);
		final TextItem activity9 = new TextItem();
		creatActivity(activity9, 9);
		final TextItem activity10 = new TextItem();
		creatActivity(activity10, 10);

		// input fields
		HeaderItem header = new HeaderItem();
		header.setDefaultValue("Create new Kanban Board");

		final TextItem boardname = new TextItem();
		boardname.setName("boardname");
		boardname.setTitle("Board Name");
		boardname.setRequired(true);

		final TextItem anzActivities = new TextItem();
		anzActivities.setName("activities");
		anzActivities.setTitle("Anzahl Aktivitäten");
		anzActivities.setRequired(true);
		anzActivities.setRedrawOnChange(true);
		anzActivities.setValue("5");

		// buttons
		createBoard = new ButtonItem();
		createBoard.setAlign(Alignment.RIGHT);
		createBoard.setName("createBoard");
		createBoard.setTitle("Create Board");
		createBoard.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (form.validate()) {
					close();
					// PreparedRequest prepReq = new
					// PreparedRequest("user", "signup",
					// "vorname", vorname
					// .getValueAsString(), "nachname",
					// nachname.getValueAsString(), "email",
					// email
					// .getValueAsString(), "password",
					// password.getValueAsString(),
					// "namensKuerzel",
					// namensKuerzel.getValueAsString());
					// prepReq.sendAsyncRequest(new
					// RequestCallback() {
					// @Override
					// public void
					// onResponseReceived(Request request,
					// Response response) {
					// SC.say("SUCCESS: " +
					// response.getText());
					// }
					//
					// @Override
					// public void onError(Request request,
					// Throwable exception) {
					//
					// }
					// });
				}
			}
		});

		// add components to form
		form.setFields(boardname, anzActivities, activity1, activity2, activity3, activity4, activity5, activity6, activity7,
				activity8, activity9, activity10);
		form.setEncoding(Encoding.MULTIPART);

		// TODO: signup ändern
		form.setAction("service/user/signup");
		form.setMethod(FormMethod.POST);

		// form properties
		setShowMinimizeButton(false);
		setAutoCenter(true);
		setWidth("350");
		setHeight("450");
		setTitle("New Kanban Board");
		setIsModal(true);
		setShowModalMask(true);
		centerInPage();

		addItem(form);
	}

	private void creatActivity(final TextItem activity, final int anz) {
		activity.setName("Akt" + anz);
		activity.setTitle("Name Aktivität " + anz);
		activity.setRequired(true);
		activity.setVisible(false);
		activity.setShowIfCondition(new FormItemIfFunction() {
			@Override
			public boolean execute(FormItem item, Object value, DynamicForm form) {
				String anzString = (String) form.getValue("activities");
				if (StringUtils.isNumeric(anzString)) {
					int anzInt = Integer.parseInt(anzString);
					if (anzInt >= anz) {
						return true;
					}
				}
				return false;
			}
		});
	}

	private void close() {
		destroy();
	}

}
